package com.lsq.oss.config;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

import java.net.URI;

public class S3Singleton {
    private S3Singleton() {
    }
    private static class SingletonHolder {
        private static final S3Singleton INSTANCE = new S3Singleton();
    }

    // 提供全局访问点
    public static S3Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    // 其他业务方法
    public S3Client getS3Client(MyOssProperties myOssProperties) {
        return S3Client.builder()
                // 关键：指定 MinIO 服务地址（覆盖默认 AWS 端点）
                .endpointOverride(URI.create(myOssProperties.getEndpoint()))
                // 配置访问凭证
                .credentialsProvider(StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(myOssProperties.getAccessKey(), myOssProperties.getSecretKey())
                ))
                // 配置区域（MinIO 不验证区域有效性，仅作标识）
                .region(Region.of(myOssProperties.getRegion()))
                // 可选：禁用 SSL 验证（本地测试用，生产环境需启用 SSL）
                .serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(myOssProperties.isSslCertification()).build())
                .build();
    }

    public S3Presigner getS3Presigner(MyOssProperties myOssProperties) {
        return  S3Presigner.builder()
                .endpointOverride(URI.create(myOssProperties.getEndpoint()))
                .credentialsProvider(StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(myOssProperties.getAccessKey(), myOssProperties.getSecretKey()
                        )))
                .region(Region.of(myOssProperties.getRegion()))
                .serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(myOssProperties.isSslCertification()).build())
                .build();
    }
}
